new Vue({
    el: '#app',
    data() {
        return {
            form: {
                Start_Date: '',
                End_Date: '',
                Article_Name: '',
                Store_SerialNumber: '',
                Enterprise_Info_SerialNumber: $.cookie('enterprise'),
                Type: 1,
                page: 1,			//页数
                limit: 10,		//每页数据条数
            },
            total: 0,// 总条数
            dealer: [],
            tableData: [],
            Data: {
                Sales_Amount: 0,
                Collection_Amount: 0
            },
            loading: false,//节流
            pickerOptions: pickerOptionsTwo,
            time: null,
            dialogVisible: false,//收款
            dialogVisible1: false,//查询
            dialogImage: false,//放大图片
            dialogInvoice:false,//发票
            dialogImageUrl: '',//图片
            imageUrl: '',//图片路径
            formData: {
                Collection_SerialNumber: '',
                Picture_URL: '',//图片地址
                type:1
            },
            loading: false,
            collection: {},//收款信息
            invoice:[],//发票图片
        }
    },
    mounted() {
        this.form.End_Date = getYMDHMSDate();
        this.form.Start_Date = getYMDHMSDate(new Date().getTime() - 30 * 24 * 60 * 60 * 1000);
        this.time = [this.form.Start_Date, this.form.End_Date]
        this.getList(this.form);
        this.getData()
    },
    computed: {
        //去.0
        ZeroRemoval() {
            return function (params) {
                params = params.replace('.0', '')
                return params
            }
        },
        //千分位处理
        thousands() {
            return function (params) {
                params = Number(params).toFixed(2)
                return params.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
            }
        },
        //百分比处理
        toPercent() {
            return function (point) {
                point = Number(point * 100).toFixed(2);
                return point += "%";
            }
        },
    },
    methods: {
        getList(data) {
            console.log(data);
            // getPurchaseReportAction
            $.ajax({
                type: 'POST',
                url: `${DomainName}/getSalesReport1Action?token=${$.cookie('token')}`,
                data,
                success: (res) => {
                    this.Data = res.Data
                    this.tableData = res.tableData
                    this.total = res.limit
                    console.log(res);
                },
                complete: (XMLHttpRequest) => {
                    this.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        },
        handleExpandChange(row) {
            $.ajax({
                type: 'POST',
                url: `${DomainName}/getSalesReportDetailAction?token=${$.cookie('token')}`,
                data: {
                    Collection_SerialNumber: row.Collection_SerialNumber,
                    Article_Name: this.form.Article_Name,
                },
                success: (Result) => {
                    row.Children.sales_order_detail = Result.Data
                },
                complete: (XMLHttpRequest) => {
                    this.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        },
        Invoicing(row) {
            $.ajax({
                type: 'POST',
                url: `${DomainName}/getSalesReportDetailAction?token=${$.cookie('token')}`,
                data: {
                    Collection_SerialNumber: row.Collection_SerialNumber,
                    Article_Name: this.form.Article_Name,
                },
                success: (Result) => {
                    var wb = XLSX.utils.json_to_sheet(Result.Data)
                    var workbook = XLSX.utils.book_new();
                    XLSX.utils.book_append_sheet(workbook, wb, "Sheet1");
                    console.log(workbook);
                    /* get binary string as output */
                    var wbout = XLSX.write(workbook, {
                        bookType: "xlsx",
                        bookSST: true,
                        type: "array"
                    });
                    try {
                        //  name+'.xlsx'表示导出的excel表格名字
                        saveAs(
                            new Blob([wbout], {
                                type: "application/octet-stream"
                            }),
                            "对账表" + ".xlsx"
                        );
                    } catch (e) {
                        if (typeof console !== "undefined") console.log(e, wbout);
                    }
                    return wbout;
                },
                complete: (XMLHttpRequest) => {
                    this.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        },
        handleInvoice(row){
            this.dialogInvoice = true
            this.invoice = []
            this.formData.Collection_SerialNumber = row.Collection_SerialNumber
        },
        submitInvoice(){
            const _this = this;
            _this.loading = true;
            let invoice = []
            this.invoice.forEach(item => {
                invoice.push(item.url)
            });
            $.ajax({
                type: 'POST',
                url: `${DomainName}/CollectionEdit?token=${$.cookie("token")}`,
                data: {
                    Picture_URL:JSON.stringify(invoice),
                    type:2,
                    Collection_SerialNumber:this.formData.Collection_SerialNumber
                },
                complete: function (XMLHttpRequest) {
                    _this.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                success: function (Result) {
                    console.log(Result);
                    if (Result.Status == 'success') {
                        _this.$message.success('上传成功')
                        _this.dialogInvoice = false;
                        _this.getList(_this.form)
                    }
                },
                error: function () {
                    _this.$message.error("操作失败，请稍后再试")
                },
                dataType: "json"
            });
        },
        uploadinvoice(file){
            let _this = this
            if (!this.loading) {
                this.loading = true
                new Compressor(file, {
                    quality: 1,
                    success(file) {
                        const formData = new FormData();
                        formData.append('file', file, file.name);
                        let oFileReader = new FileReader();
                        oFileReader.readAsDataURL(file);
                        oFileReader.onload = function (e) {
                            $.ajax({
                                type: 'POST',
                                url: `${DomainName}/getElectronicPIC?token=${$.cookie("token")}`,
                                data: {
                                    Data: e.target.result,
                                    DataLength: file.size,
                                    FileName: file.name
                                },
                                complete: function (XMLHttpRequest) {
                                    _this.loading = false
                                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                                    }
                                },
                                success: function (Result) {
                                    console.log(Result)
                                    if (Result.Status == "success") {
                                        _this.invoice.push({
                                            src:e.target.result,
                                            url:Result.Picture_Url
                                        })
                                        _this.$message.success('上传成功')
                                    } else if (Result.Status == "fail") {
                                        _this.$message.error(Result.Cause + ',请重新上传!')
                                    } else {
                                        _this.$message.error('上传失败')
                                    }
                                },
                                error: function () {
                                    _this.$message.error("操作失败，请稍后再试")
                                },
                                dataType: "json"
                            });
                        }
                    },
                    error(err) {
                        console.log(err.message);
                    },
                });
            }
        },
        handleCollection(row) {
            this.formData.Collection_SerialNumber = row.Collection_SerialNumber
            this.imageUrl = '';
            this.formData.Picture_URL = '';
            this.dialogVisible = true
        },
        handlePictureCardPreview(file) {
            this.dialogImage = true
            this.dialogImageUrl = file
        },
        uploadImg(file) {
            let _this = this
            if (!this.loading) {
                this.loading = true
                new Compressor(file, {
                    quality: 1,
                    success(file) {
                        const formData = new FormData();
                        formData.append('file', file, file.name);
                        let oFileReader = new FileReader();
                        oFileReader.readAsDataURL(file);
                        oFileReader.onload = function (e) {
                            $.ajax({
                                type: 'POST',
                                url: `${DomainName}/getElectronicPIC?token=${$.cookie("token")}`,
                                data: {
                                    Data: e.target.result,
                                    DataLength: file.size,
                                    FileName: file.name
                                },
                                complete: function (XMLHttpRequest) {
                                    _this.loading = false
                                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                                    }
                                },
                                success: function (Result) {
                                    console.log(Result)
                                    if (Result.Status == "success") {
                                        _this.imageUrl = e.target.result
                                        _this.formData.Picture_URL = Result.Picture_Url
                                        _this.$message.success('上传成功')
                                    } else if (Result.Status == "fail") {
                                        _this.$message.error(Result.Cause + ',请重新上传!')
                                    } else {
                                        _this.$message.error('上传失败')
                                    }
                                },
                                error: function () {
                                    _this.$message.error("操作失败，请稍后再试")
                                },
                                dataType: "json"
                            });
                        }
                    },
                    error(err) {
                        console.log(err.message);
                    },
                });
            }
        },
       
        submitImg() {
            const _this = this;
            _this.loading = true;
            $.ajax({
                type: 'POST',
                url: `${DomainName}/CollectionEdit?token=${$.cookie("token")}`,
                data: this.formData,
                complete: function (XMLHttpRequest) {
                    _this.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                success: function (Result) {
                    if (Result.Status == 'success') {
                        _this.$message.success('上传成功')
                        _this.dialogVisible = false;
                        _this.getList(_this.form)
                    }
                },
                error: function () {
                    _this.$message.error("操作失败，请稍后再试")
                },
                dataType: "json"
            });
        },
        handleRemove() {
            this.imageUrl = '';
            this.formData.Picture_URL = '';
        },
        handle(row) {
            let _this = this
            $.ajax({
                type: 'POST',
                url: `${DomainName}/getInquiryIDAction?token=${$.cookie("token")}`,
                data: {
                    Data: 'collection_invoice',
                    Primary_Key_Name:'Collection_SerialNumber',
                    Primary_Key:row.Collection_SerialNumber,
                    isNullEnterprise:true
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                success: function (Result) {
                    console.log(Result);
                    _this.collection = row
                    if (Result.length>0) {
                        _this.$set(_this.collection, 'invoice', Result)
                    }
                    _this.dialogVisible1 = true
                },
                error: function () {
                    _this.$message.error("操作失败，请稍后再试")
                },
                dataType: "json"
            });
        },
        getData() {
            $.ajax({
                type: 'POST',
                url: `${DomainName}/getSales_Store?token=${$.cookie('token')}`,
                data: {
                    Enterprise_Info_SerialNumber: $.cookie('enterprise')
                },
                success: (res) => {
                    this.dealer = res.Data
                },
                complete: (XMLHttpRequest) => {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        },
        handleclassify() {//分类查询
            this.page = 1;
            this.getList(this.form);
        },
        DateChange() {
            this.page = 1;
            if (this.time != null) {
                this.form.Start_Date = this.time[0]
                this.form.End_Date = this.time[1]
            } else {
                this.form.Start_Date = ''
                this.form.End_Date = ''
            }
            this.getList(this.form);
        },
        handleSizeChange(val) {//分页查询
            this.form.page = 1;
            this.form.limit = val;
            this.getList(this.form)
        },
        handleCurrentChange(val) {
            this.form.page = val;
            this.getList(this.form)
        },
    },
})